extends Node2D


# Called when the node enters the scene tree for the first time.
func _ready():
	pass # Replace with function body.

var restarting = false

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	
	if not $Player_Blue:
		return
	
	if not $Player_Blue.died and not $Player_Red.died:
		$Camera2D.position = ($Player_Blue.position + $Player_Red.position)/2
		
		
		
		var size = ($Camera2D.get_window().size)
		
		
		
	else:
		if not restarting:
			restart()
		for e in [$Player_Blue,$Player_Red]:
			if not e.died:
				$Camera2D.position = e.position

func restart():
	restarting = true
	
	if $Player_Blue.died:
		%GUI.add_score(1,1)
	if $Player_Red.died:
		%GUI.add_score(2,1)
	
	await get_tree().create_timer(3).timeout
	var epos = [$PlayerBlueStartPoint,$PlayerRedStartPoint]
	var index = 0
	for ep in [$Player_Blue,$Player_Red]:
		ep.show()
		ep.position = epos[index].position
		ep.died = false
		index+=1
		for eb in get_tree().get_nodes_in_group("bullet"):
			eb.queue_free()
		ep.has_bullet = true
	restarting = false

func _input(event):
	if event.is_action_released("restart"):
		get_tree().reload_current_scene()
